package my.Views;

import java.util.ArrayList;

import my.eightZOne.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

public class explainView extends View implements OnGestureListener{
	public static final int MESSAGE_CLIP = 100;
	public static final int REFRESH = 20;
	private Paint mPaint;
	private Path mPath;
	private int pointCount = 10;
	private int index = 0;
	private float offset = 20;
	private ArrayList<PointF> eventPointList = new ArrayList<PointF>();
	private ArrayList<PointF> pointPath = new ArrayList<PointF>();
	private CornerPathEffect mCornerPathEffect = new CornerPathEffect(10);
	
	Bitmap chessboard = BitmapFactory.decodeResource(getResources(), R.drawable.instructionindex);
	Bitmap chessboardF;
	private float Sheight;		//屏幕宽度
	private float Swidth;		//屏幕高度
	private float HeightScale;		//屏幕宽度与预设比例
	private float WidthScale;		//屏幕高度与预设比例
	
	
	public explainView(Context context) {
		super(context);
		init();
		DisplayMetrics dm = getResources().getDisplayMetrics();
		Swidth = (float)dm.widthPixels;
		Sheight = (float)dm.heightPixels;
		WidthScale = Swidth/480;		//屏幕宽度与预设比例
		HeightScale = Sheight/800;	//屏幕高度与预设比例
		int bgWidth = chessboard.getWidth();
		int bgHeight = chessboard.getHeight();
		chessboardF = scaleImg(chessboard, WidthScale * 480 / bgWidth, HeightScale * 800 / bgHeight);
//		chessboardF = scaleImg(chessboard, WidthScale, HeightScale);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX(0);
		float y = event.getY(0);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			index = 0;
			myHandler.removeMessages(MESSAGE_CLIP);
			index++;
			eventPointList.clear();
			eventPointList.add(new PointF(x, y));
			break;
		case MotionEvent.ACTION_MOVE:
			
			PointF eventMove = new PointF(x, y);
			if (index>0 &&  distance(eventMove, eventPointList.get(index - 1))> 10.0f) {
				if (index > pointCount) {
					eventPointList.add(new PointF(x, y));
					eventPointList.remove(0);
				} else {
					eventPointList.add(new PointF(x, y));
					index++;
				}

			}else{
				if(index>4){
	 				eventPointList.remove(0);
	 				index--;
	 			}
			}
			invalidate();

			break;
		case MotionEvent.ACTION_UP:
			myHandler.sendEmptyMessageDelayed(MESSAGE_CLIP, 100);
			break;

		}
		return true;
	}

	private void init() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setPathEffect(mCornerPathEffect);
		mPaint.setColor(0xCCFFFF00);
		mPath = new Path();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		canvas.drawBitmap(chessboardF, 0, 0, mPaint);		//绘制棋盘背景
		drawLine(canvas);
	}

	private void drawLine(Canvas canvas) {
		
		if (index < 2) {
			return;
		} else {
			float w = offset / ((index < pointCount?index:pointCount)+5);
			PointF tempLast = eventPointList.get(index - 1);
			float k = getK(tempLast, eventPointList.get(index - 2));
			PointF lastPoint = new PointF();
			lastPoint.x = (float) (tempLast.x + Math.cos(k) * 10);
			lastPoint.y = (float) (tempLast.y + Math.sin(k) * 10);
			pointPath.add(lastPoint);
			for (int i = index - 2; i > 0; i--) {
				float kLeft = getK(eventPointList.get(i),
						eventPointList.get(i - 1));
				PointF temp = new PointF();
				temp.x = (float) (eventPointList.get(i).x - Math.sin(kLeft) * w
						* i);
				temp.y = (float) (eventPointList.get(i).y + Math.cos(kLeft) * w
						* i);
				pointPath.add(0,temp);
			}
			pointPath.add(0,eventPointList.get(0));
			pointPath.add(lastPoint);
			for (int i = index - 2; i > 0; i--) {
				float kRight = getK(eventPointList.get(i),
						eventPointList.get(i - 1));
				PointF temp = new PointF();
				temp.x = (float) (eventPointList.get(i).x + Math.sin(kRight) * w
						* i);
				temp.y = (float) (eventPointList.get(i).y - Math.cos(kRight) * w
						* i);
				pointPath.add(temp);
			}
			pointPath.add(eventPointList.get(0));
			drawLineByPoints(canvas);

		}
	}

	public float distance(PointF p1, PointF p2) {
		float tt = (float) Math.pow((p1.x - p2.x) * (p1.x - p2.x)
				+ (p1.y - p2.y) * (p1.y - p2.y), 0.5);
		return tt;

	}

	public float getK(PointF p1, PointF p2) {
		return (float) Math.atan2(p1.y - p2.y, p1.x - p2.x);
	}

	public void drawLineByPoints(Canvas canvas) { 
		int pathSize = index * 2;
		mPath.reset();
		mPath.moveTo(pointPath.get(0).x, pointPath.get(0).y);
		for (int i = 1; i < pathSize; i++) {
			mPath.lineTo(pointPath.get(i).x, pointPath.get(i).y);
		}
		//带填充色的path
		canvas.drawPath(mPath, mPaint);
		//不带填充色的path
		//canvas.drawPath(mPath, mStrokePaint);
		pointPath.clear();

	}
	private Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case MESSAGE_CLIP:
				if (index>1) {
					eventPointList.remove(0);
					index--;
					this.sendEmptyMessageDelayed(MESSAGE_CLIP, REFRESH);
				} else {
					index = 0;
					eventPointList.clear();
					return;
				}
				invalidate();
				
				break;
			}

		}

	};

	public void clear() {
		mPath.reset();
		mPath = null;
		eventPointList.clear();
		eventPointList = null;
		eventPointList = null;
		eventPointList = null;
		mCornerPathEffect = null;
		mPaint = null;
		myHandler = null;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}
	//缩放图片函数
	public Bitmap scaleImg(Bitmap bm, float scaW, float scaH) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
       }
}